﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Reflection;
using System.Web;
using System.Web.Mvc;

namespace AspDotNet.Framework.Mvc.FirstDemo.Controllers
{
    public class ErrorHandler1Controller : Controller
    {
        public ActionResult Index()
        {
            throw new NotImplementedException();
        }

        protected override void OnException(ExceptionContext filterContext)
        {
            filterContext.ExceptionHandled = true;

            //Log the error!!

            //Redirect to action
            filterContext.Result = RedirectToAction("Home", "Index");

            // OR return specific view
            string controllerName = (string)filterContext.RouteData.Values["controller"];
            string actionName = (string)filterContext.RouteData.Values["action"];
            var view = new ViewResult
            {
                ViewName = "~/Views/Shared/InternalError.cshtml",
                ViewData = new ViewDataDictionary<HandleErrorInfo>(new HandleErrorInfo(filterContext.Exception, controllerName, actionName)),
            };
           view.ViewBag.Exception = filterContext.Exception;

            filterContext.Result = view;
        }
    }
}